﻿<%@ Page Language="VB" AutoEventWireup="false" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html>
<script runat="server">

    Public ForcedAccount As Boolean
    Public Msg As String = ""
    Public PersonsId As Integer

    ' true se autenticato , false se no
    ' in Msg il messaggio da restiruire al client 
    ' in PersonsId restituisce il codice dell'utente
    Public Function CheckLogin(ByRef Msg As String, ByRef PersonsId As Integer) As Boolean

        ' utilizzo di datareader            
        Dim query As String
        Dim conn As SqlConnection
        Dim command As SqlCommand
        Dim reader As SqlDataReader

        conn = New SqlConnection(ConfigurationManager.ConnectionStrings("MSSql12155ConnectionString").ConnectionString)
        
        ' se valore nullo esci
        If Request("userid") = Nothing Then
            Msg = "User o password non riconosciuti"
            Return False
        End If
            
        query = "SELECT Persons_Id, active, ForcedAccount, Name FROM Persons WHERE [userId]='" & Request("userid") & "' AND [password]='" & Request("password") & "'"
        command = New SqlCommand(query, conn)
        conn.Open()

        reader = command.ExecuteReader()

        If reader.HasRows Then
            ' autenticazione avvenuta
            reader.Read()
           
            ' utente deve avere limitazione su progetti (ForcedAccount = true)
            If reader.GetBoolean(2) = False Then
                Msg = "Utente non valido, mancano limitazioni su progetti"
                Return False
            End If

            ' colonna 9 contiene il flag di attivo
            If reader.GetBoolean(1) Then

                ' salva coockie *****
                If RbCookie.Checked Then
                    Response.Cookies("userName").Value = Request("userid")
                Else
                    Response.Cookies("userName").Value = ""
                End If
                            
                Response.Redirect("menu.aspx?id=" & reader.GetInt32(0))
                Return True
            Else
                Msg = "Utente non attivo"
                Return False
            End If

        Else
            Msg = "User o password non riconosciuti"
            ' utente non autenticato
            Return False
        End If
                
        conn.Close()
        
    End Function


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        '  Default userid
        If Not Request.Cookies("userName") Is Nothing Then
            Dim aCookie As HttpCookie = Request.Cookies("userName")
            userid.Text = Server.HtmlEncode(aCookie.Value)
        End If
        
    End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Login" runat="server">
    <title>Login</title>

    <meta name="viewport" content="initial-scale=1">
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <!-- style sheets -->
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
    <link rel="stylesheet" href="./css/TimereportMobile.css" />
    <!-- jquery mobile -->
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="./js/customscript.js"></script> 
    <script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
    <!-- jquery mobile FINE -->
    <script>
    <!-- elimina barra -->   
            var iWebkit; if (!iWebkit) { iWebkit = window.onload = function () { function fullscreen() { var a = document.getElementsByTagName("a"); for (var i = 0; i < a.length; i++) { if (a[i].className.match("noeffect")) { } else { a[i].onclick = function () { window.location = this.getAttribute("href"); return false } } } } function hideURLbar() { window.scrollTo(0, 0.9) } iWebkit.init = function () { fullscreen(); hideURLbar() }; iWebkit.init() } }
     </script>
    <script type="text/javascript">
        if (navigator.cookieEnabled) {
            // cookie attivi
        }
        else
        // cookie non attivi
            $(document).ready(function () {
                $('#CookieMessage').show(); /*mostra messagggio*/

                // elimina form
                $(document).ready(function () {
                    $('#form1').remove();
                });
            });
            
    </script>
</head>
<body>
    <div data-role="page" >
        <div data-role="header" data-position="fixed">
            <h1>
                Timereport Login</h1>
        </div>
        <!-- /header -->        
        <br />
        <div>
            <div id="CookieMessage" class='ui-body ui-body-e'>L'applicazione Timereport richiede l'attivazione dei <b>cookies</b> (Impostazioni->Safari)</div>
            <br />
            <%  
                If CheckLogin(Msg, PersonsId) = False And Page.IsPostBack = True Then
                    Response.Write("<div class='ui-body ui-body-e'>" & Msg & "</br></div>")
                End If
            %>
        <form id="form1" runat="server">
            <asp:TextBox ID="userid" runat="server" placeholder="Username"></asp:TextBox>
            <br />
            <input type="password" name="password" id="password" value="" placeholder="Password" />
            <br />
            <asp:CheckBox ID="RbCookie" Text="Ricorda la user" runat="server" Checked="True"
                data-inline="true" />
            <br />
            <asp:Button ID="Button1" type="submit" runat="server" data-role="button" data-inline="true"
                data-theme="b" Text="Login" />
            &nbsp;</div>
        </form>

        <div data-role="footer" style="position:absolute; width:100%;bottom: 0; left:0;">
            <h4>
                Aeonvis @2012 - mTimeReport &nbsp;
                <img src="./images/beta_icon2.png" /></h4>
        </div>
        <!-- /footer -->
    </div>
    <!-- /page -->
</body>
</html>
